package com.yanqu.road.server.cmd.cricket;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.unionwar.UserCricketSendTimes;
import com.yanqu.road.entity.cricket.CricketData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.RankUnionWarProto;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.cricket.CricketModule;
import com.yanqu.road.server.manger.activity.rankunionwar.RankUnionWarMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionPhaseWarMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionWar;
import com.yanqu.road.server.manger.activity.unionwar.UnionWarActivityMgr;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：zfr
 * @date ：2021/11/5 17:08
 */
@Cmd(code = Protocol.S_CRICKET_ACTIVITY_WAR_TIMES, desc = "昆虫类的活动出战次数")
public class CricketActivityWarTimesCmd extends AbstractCommand {
    private int activityId;
    private int activityType;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CricketProto.CricketActivityWarTimesReqMsg reqMsg = CricketProto.CricketActivityWarTimesReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        activityType = reqMsg.getActivityType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (eActivityType.CrossUnionWarActivity.getValue() == activityType) {
            ActivityInfo activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
            if (activityInfo == null) {
                return;
            }
        } else if(eActivityType.RankUnionWar.getValue() == activityType){
            if(RankUnionWarMgr.getActivityId() != activityId){
                return;
            }
        }else {
            return;
        }

        Map<Integer, CricketData> userCricketMap = player.getModule(CricketModule.class).getCricketDataMap();
        List<CricketData> userCricketList = new ArrayList<>(userCricketMap.values());
        if (eActivityType.CrossUnionWarActivity.getValue() == activityType) {
            Map<Integer, UserCricketSendTimes> userCricketSendTimesMap = null;
            UnionWar unionWar = UnionPhaseWarMgr.getUnionWar(activityId);
            if (unionWar != null) {
                if (unionWar.getUserCricketSendMap().containsKey(player.getUserId())) {
                    userCricketSendTimesMap = unionWar.getUserCricketSendMap().get(player.getUserId());
                }
            }
            CricketProto.CricketActivityWarTimesRespMsg.Builder respMsg = CricketProto.CricketActivityWarTimesRespMsg.newBuilder();
            for (CricketData cricketData : userCricketList) {
                CricketProto.CricketActivityWarTimesTempMsg.Builder temp = CricketProto.CricketActivityWarTimesTempMsg.newBuilder();
                temp.setCricketId(cricketData.getCricketId());
                if (userCricketSendTimesMap != null && userCricketSendTimesMap.containsKey(cricketData.getCricketId())) {
                    UserCricketSendTimes sendTimes = userCricketSendTimesMap.get(cricketData.getCricketId());
                    temp.setWarTimes(sendTimes.getTimes());
                } else {
                    temp.setWarTimes(0);
                }
                respMsg.addTimes(temp);
            }
            respMsg.setRet(0);
            respMsg.setActivityId(activityId);
            respMsg.setActivityType(activityType);
            player.sendPacket(Protocol.U_CRICKET_ACTIVITY_WAR_TIMES, respMsg);
        }else if(eActivityType.RankUnionWar.getValue() == activityType){
            RankUnionWarProto.CrossCricketActivityWarTimesReqMsg.Builder reqMsg = RankUnionWarProto.CrossCricketActivityWarTimesReqMsg.newBuilder();
            for(CricketData cricketData : userCricketList){
                CricketProto.CricketActivityWarTimesTempMsg.Builder temp = CricketProto.CricketActivityWarTimesTempMsg.newBuilder();
                temp.setCricketId(cricketData.getCricketId());
                temp.setWarTimes(0);
                reqMsg.addTimes(temp);
            }
            reqMsg.setActivityId(activityId);
            reqMsg.setActivityType(activityType);
            player.sendPacket(Protocol.C2_CROSS_RANKUNIONWAR_CRICKET_TIMES, reqMsg);
        }
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
